package com.zhangyuji.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhangyuji.common.R;
import com.zhangyuji.entity.ShoppingCart;
import com.zhangyuji.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartControlller {

    @Resource
    private ShoppingCartService shoppingCartService;

    /**
     * 展示菜品/套餐到购物车
     *
     * @param request
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> getList(HttpServletRequest request) {
        //获取本次登录用户的手机号
        String userPhone = (String) request.getSession().getAttribute("user");
        Long userId = shoppingCartService.getUserId(userPhone);


        LambdaQueryWrapper<ShoppingCart> shoppingCartWrapper = new LambdaQueryWrapper<>();
        shoppingCartWrapper.eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(shoppingCartWrapper);
        return R.success(shoppingCarts);
    }

    /**
     * 添加菜品/套餐到购物车
     *
     * @param shoppingCart
     * @param request
     * @return
     */

    @PostMapping("/add")
    public R<String> addToShoppingCart(@RequestBody ShoppingCart shoppingCart, HttpServletRequest request) {
        log.info("shoppingCart={}", shoppingCart);
        //获取本次登录用户的手机号
        String userPhone = (String) request.getSession().getAttribute("user");
        //根据手机号查询用户id
        Long userId = shoppingCartService.getUserId(userPhone);

        shoppingCart.setUserId(userId);
        shoppingCart.setCreateTime(LocalDateTime.now());

        //查询数据库是否存在当前添加到购物车的菜品
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getName, shoppingCart.getName());
        ShoppingCart shoppingCartServiceOne = shoppingCartService.getOne(queryWrapper);

        if (shoppingCartServiceOne != null) {
            shoppingCartServiceOne.setNumber(shoppingCartServiceOne.getNumber() + 1);
            boolean status = shoppingCartService.updateById(shoppingCartServiceOne);
            return status ? R.success("保存成功") : R.error("保存失败");
        }

        //将shoppingCart对象保存到数据库
        boolean flag = shoppingCartService.save(shoppingCart);
        return flag ? R.success("保存成功") : R.error("保存失败");
    }


    /**
     * 从购物车删除菜品/套餐
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<List<ShoppingCart>> subToShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
                    //当前为菜品
        queryWrapper.eq(shoppingCart.getDishId() != null,ShoppingCart::getDishId, dishId)
                    //当前为套餐
                    .eq(shoppingCart.getSetmealId() != null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        ShoppingCart shoppingCartServiceOne = shoppingCartService.getOne(queryWrapper);

        //表示当前菜品/套餐有多个
        if (shoppingCartServiceOne.getNumber() > 1 ) {

            shoppingCartServiceOne.setNumber(shoppingCartServiceOne.getNumber() - 1);

            //更新购物车
            shoppingCartService.updateById(shoppingCartServiceOne);
        }else {
            //表示当前菜品/套餐只有一个
            shoppingCartService.removeById(shoppingCartServiceOne.getId());
        }

        //查询当前购物车
        LambdaQueryWrapper<ShoppingCart> currentWrapper = new LambdaQueryWrapper<>();
        currentWrapper.eq(ShoppingCart::getUserId, shoppingCartServiceOne.getUserId());
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(currentWrapper);
        return R.success(shoppingCartList);

    }

    @DeleteMapping("/clean")
    public R<List<ShoppingCart>> cleanShoppingCart(HttpServletRequest request){
        //获取用户手机号
        String userPhone = (String) request.getSession().getAttribute("user");
        //获取用户id
        Long userId = shoppingCartService.getUserId(userPhone);
        //根据用户id删除该用户的购物车信息
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(wrapper);
        //查询当前购物车
        wrapper.eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(wrapper);
        return R.success(shoppingCartList);
    }
}
